﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TF.Access;
using System.Collections.Generic;
using System.Text;
using SystemFramework.Util;

public partial class TripPlan : System.Web.UI.Page
{
    protected string eventTitle;
    protected string eventPic;
    protected string eventDate;
    protected string attendCount;
    protected string attentionCount;
    protected string eventCost;
    protected string fromCity;
    protected string toCities;
    protected string tripOwner;
    protected string tripOwnerId;
    protected string QQ;
    protected string mobilePhone;
    protected string trips;
    protected string eventHMemo;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request["ehId"]))
            {
                int ehId = int.Parse(Request["ehId"]);
                int totalDays = 0;
                TF_EventH h = TF_EventH.Select(ehId);

                attendCount = ConvertHelper.GetString(h.AttendCount,string.Empty);
                attentionCount = ConvertHelper.GetString(h.AttentionCount, string.Empty);

                TF_UserProfile currentUser = TF_UserProfile.Select(h.OwnerId);
                eventTitle = h.EventTitle;
                eventPic = h.PicUrl;
                List<TF_EventDetail> eds = TF_EventDetail.SelectByEhId(ehId);
                eventCost = "￥" + h.TotalCost.ToString("F");//eg:33.33
                fromCity = TF_City.Select(h.StartCityId).CityName;
                string seprator = " | ";

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < eds.Count; i++)
                {
                    TF_EventDetail ed = eds[i];
                    if (ed.DayNumber > totalDays)
                    {
                        totalDays = ed.DayNumber;
                    }
                    TF_City tfCity = TF_City.Select(ed.ToCityId);
                    sb.Append(tfCity.CityName).Append(seprator);
                }
                eventDate = h.CreateDate.ToString("yyyy年MM月dd日") + string.Format("(共{0}天)", totalDays);
                if (sb.Length > 0)
                {
                    toCities = sb.ToString(0, sb.Length - seprator.Length);
                }
                if (currentUser != null)
                {
                    tripOwner = currentUser.Nick_Name;
                    tripOwnerId = currentUser.Uid.ToString();
                    QQ = currentUser.QQ;
                    mobilePhone = currentUser.MobilePhone;
                }
                eventHMemo = h.Memo;
                //trips = GetTrips(ehId);
                trips = WSLogin.GetTripListsByEhId(ehId);
            }
        }
    }

    public string GetTrips(int ehId)
    {
        string delTripPlan = string.Empty;
        string delCity = string.Empty;
        string delView = string.Empty;
        string showAddPlan = string.Empty;
        TF_EventH h = TF_EventH.Select(ehId);
        // 本人可以修改
        TF_UserProfile up = null;
        if (WebUtility.IsLogin())
        {
            up = WebUtility.GetUserProfile();
            //if (up.Uid == h.OwnerId)
            //{
            //    delCity = "<img src='images/ico_close.gif' onclick='delEventDetail({0});return false' />";
            //    delView = "<img src='images/ico_close.gif' onclick='delEventView({0});return false' />";
            //    delTripPlan = "<img src='images/ico_close.gif' onclick='delEventH({0});return false' />";
            //    showAddPlan = "<a href='#' onclick='alertWin(\"addtripboxCloseBtn\",\"addtripbox\",\"addtripboxTitle\",553,457);return false;'><img src='images/add.gif' /></a>";
            //}
        }
        int totalDays = 0;
        
        List<TF_EventDetail> eds = TF_EventDetail.SelectByEhId(ehId);
        StringBuilder tripsBuilder = new StringBuilder();
        for (int i = 0; i < eds.Count; i++)
        {
            TF_EventDetail ed = eds[i];
            TF_City tfCity = TF_City.Select(ed.ToCityId);

            if (up != null && up.Uid == h.OwnerId)
            {
                delCity = string.Format("<img src='images/ico_close.gif' onclick='delEventDetail({0});return false' />", ed.Id);
                delTripPlan = string.Format("<img src='images/ico_close.gif' onclick='delEventDetailByDayNum({0},{1});return false' />", ehId, ed.DayNumber);
                showAddPlan = "<a href='#' onclick='alertWin(\"addtripboxCloseBtn\",\"addtripbox\",\"addtripboxTitle\",553,457);return false;'><img src='images/add.gif' /></a>";
            }

            string tripCity = string.Format(Constant.TRIP_CITY, ed.Id, tfCity.CityName, delCity);

            List<TF_EventView> tfEventViews = TF_EventView.SelectEventViewByCityId(ed.ToCityId);
            StringBuilder viewsBuilder = new StringBuilder();
            foreach (TF_EventView ev in tfEventViews)
            {
                if (up != null && up.Uid == h.OwnerId)
                {
                    delView = string.Format("<img src='images/ico_close.gif' onclick='delEventView({0});return false' />",ev.Id);
                }
                viewsBuilder.Append(string.Format(Constant.TRIP_VIEW, ed.ToCityId, ev.Id, TF_View.Select(ev.ViewId).Name, delView));
            }

            StringBuilder timeBoxBuilder = new StringBuilder();
            timeBoxBuilder.Append(string.Format(Constant.TIME_BOX, ed.ToCityId, tripCity + viewsBuilder.ToString()));
            totalDays = ed.DayNumber;

            // Next ed
            i++;
            while (i < eds.Count)
            {
                TF_EventDetail nextEd = eds[i];

                //相等，放在同一天
                if (ed.DayNumber == nextEd.DayNumber)
                {
                    // set total days
                    if (totalDays < nextEd.DayNumber)
                    {
                        totalDays = nextEd.DayNumber;
                    }

                    TF_City nexttfCity = TF_City.Select(ed.ToCityId);
                    string nexttripCity = string.Format(Constant.TRIP_CITY, nextEd.Id, nexttfCity.CityName, delCity);

                    List<TF_EventView> nexttfEventViews = TF_EventView.SelectEventViewByCityId(nextEd.ToCityId);
                    StringBuilder nextviewsBuilder = new StringBuilder();
                    foreach (TF_EventView ev in nexttfEventViews)
                    {
                        nextviewsBuilder.Append(string.Format(Constant.TRIP_VIEW, nextEd.ToCityId, ev.Id, TF_View.Select(ev.ViewId).Name, delView));
                    }

                    //nextviewsBuilder.Append(string.Format(Constant.TIME_BOX, nextEd.ToCityId, nexttripCity + nextviewsBuilder.ToString()));

                    timeBoxBuilder.Append(string.Format(Constant.TIME_BOX, nextEd.ToCityId, nexttripCity + nextviewsBuilder.ToString()));
                    ed = nextEd;

                    i++;
                    continue;
                }
                else
                {
                    i--;
                    break;
                }
            }

            tripsBuilder.Append(string.Format(Constant.TRIP_PLAN, 
                ed.DayNumber, h.CreateDate.ToString("yyyy年MM月dd日") + string.Format("(第{0}天)", ed.DayNumber), timeBoxBuilder.ToString(),
                delTripPlan,
                showAddPlan));
        }

        return tripsBuilder.ToString();
    }
}
